﻿using System;
using System.Windows.Forms;
using Softyum.Common.Forms;
using System.Threading;
using Softyum.ForexAgent.Config;
using Softyum.ForexAgent.Forms;
using Softyum.ForexAgent.Site;

namespace Softyum.ForexAgent
{
    static class Program
    {
        public static ConfigManager ConfigManager { get; private set; }
        public static NewsSourceManager NewsSourceManager { private set; get; }

        public static Form MainForm { get; set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += 
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += 
                new ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //
            ConfigManager = new ConfigManager();
            ConfigManager.Reload();

            //
            NewsSourceManager = new Site.NewsSourceManager();
            //
            Program.MainForm = new MainForm();
            Application.Run(Program.MainForm);
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            try
            {
                ErrorBox.Show(e.Exception.Message + e.Exception.StackTrace, Application.ProductName);
            }
            finally
            {
                Application.Exit();
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                ErrorBox.Show(ex.Message + ex.StackTrace, Application.ProductName);
            }
            finally
            {
                //Application.Exit();
                Environment.Exit(-1);
            }
        }
    }
}
